#include "MainWindow.h"

void MainWindow::addSprite(const SDL_Rect &rect)
{
    Gtk::TreeModel::iterator iter = m_ccbImages.get_active();
    if (iter) {
        Gtk::TreeModel::Row imageInfo = *iter;

        Glib::RefPtr<Gtk::ListStore> spriteRef = imageInfo[m_imageListItem.m_refSpriteList];

        Gtk::TreeModel::Row spriteInfo = *(spriteRef->append());

        uint sic = imageInfo[m_imageListItem.m_spriteIndexCounter];
        spriteInfo[m_spriteListItem.m_iId] = sic++;
        imageInfo[m_imageListItem.m_spriteIndexCounter] = sic;

        spriteInfo[m_spriteListItem.m_rect] = rect;

        m_ccbSprites.set_active(spriteInfo);

        if (Debug::test(DEBUG_ADD_REMOVE)) {
            cout << "Added sprite " << rect.x << " " << rect.y << " " << rect.w << " " << rect.h << " " << sic << endl;
        }
        Glib::RefPtr<Gdk::Pixbuf> image = imageInfo[m_imageListItem.m_refImage];
        m_csaSprite.setImage(image, rect.x, rect.y, rect.w, rect.h);
        m_imgList.getSpriteList(imageInfo[m_imageListItem.m_iId]).add(imageInfo[m_imageListItem.m_iId], spriteInfo[m_spriteListItem.m_iId], rect);

    }
}


MainWindow::MainWindow() :
    m_ccbSpriteVisible("Visible"),
    m_ccbSpriteSolid("Solid"),
    m_cbAddSpriteToScene("Add Sprite to Scene"),
    m_tool_new(Gtk::Stock::NEW),
	m_tool_open(Gtk::Stock::OPEN),
	m_tool_save(Gtk::Stock::SAVE),
	m_clImageLabel("IMAGE"),
	m_tool_add_image(Gtk::Stock::ADD),
	m_tool_remove_image(Gtk::Stock::REMOVE),
	m_clSpriteLabel("SPRITE"),
	m_tool_add_sprite(Gtk::Stock::ADD),
	m_tool_remove_sprite(Gtk::Stock::REMOVE),
    m_cafDrawingAreaAspect("Image", Gtk::ALIGN_TOP, Gtk::ALIGN_LEFT, 1.0, false),
    m_cfAddToFrame("Add To"),
    m_crbAddToLeft("Left"),
    m_crbAddToRight("Right"),
    m_crbAddToDown("Down"),
    m_crbAddToUp("Up"),
    m_crbAddToTopLeft("Top Left"),
    m_crbAddToTopRight("Top Right"),
    m_crbAddToBottomLeft("Bottom Left"),
    m_crbAddToBottomRight("Bottom Right"),
    m_iAddSpriteSide(RIGHT),
    m_imageCount(0),
    m_imageIndexCounter(0),
    m_imgList(*(ImageList::get_instance())),
    m_layerList(*(Layer_List::get_instance())),
    m_currentCtx(*(CurrentContext::get_instance())),
    m_strLastVisitedFolder(".")
{
    set_title("JSR");
    set_border_width(10);
//    set_default_size(400, 400);

    add(m_cvbWindowArea);

    m_ctbStdToolBar.append(m_tool_new);
    m_ctbStdToolBar.append(m_tool_separator);
    m_ctbStdToolBar.append(m_tool_open);
    m_ctbStdToolBar.append(m_tool_save);

    m_tool_image_label.add(m_clImageLabel);
    m_ctbImageToolBar.append(m_tool_image_label);
    m_tool_image_combo.add(m_ccbImages);
    m_ctbImageToolBar.append(m_tool_image_combo);
    m_ctbImageToolBar.append(m_tool_add_image);
    m_ctbImageToolBar.append(m_tool_remove_image);

    m_tool_sprite_label.add(m_clSpriteLabel);
    m_ctbSpriteToolBar.append(m_tool_sprite_label);
    m_tool_sprite_combo.add(m_ccbSprites);
    m_ctbSpriteToolBar.append(m_tool_sprite_combo);
    m_ctbSpriteToolBar.append(m_tool_add_sprite);
    m_ctbSpriteToolBar.append(m_tool_remove_sprite);

    m_cvbToolbarBox.pack_start(m_ctbStdToolBar);
    m_cvbToolbarBox.pack_start(m_ctbImageToolBar);
    m_cvbToolbarBox.pack_start(m_ctbSpriteToolBar);
    m_cvbWindowArea.pack_start(m_cvbToolbarBox);

    m_cvbWindowArea.pack_start(m_chbClientArea);

    m_tool_add_sprite.set_expand(false);

    m_chbClientArea.pack_start(m_cvbLeftPan);
    m_chbClientArea.pack_start(m_cvbRightPan);

    m_cafDrawingAreaAspect.add(m_cMyDrawingArea);

    m_cvbLeftPan.pack_start(m_cafDrawingAreaAspect);

    m_csaSprite.set_size_request(100, 100);
    m_cvbRightPan.pack_start(m_csaSprite);

    m_ccbSpriteSolid.set_active(true);
    m_ccbSpriteVisible.set_active(true);

    m_chbSpriteInstaceAttibutBox.pack_start(m_ccbSpriteVisible);
    m_chbSpriteInstaceAttibutBox.pack_start(m_ccbSpriteSolid);

    m_cvbRightPan.pack_start(m_chbSpriteInstaceAttibutBox);

    m_cvbRightPan.pack_start(m_cbAddSpriteToScene);

    m_cvbRightPan.pack_start(m_cfAddToFrame);

    m_cvbRightPan.pack_start(m_layerListUi);

    m_cMyDrawingArea.set_size_request(300, 300);

    Gtk::RadioButton::Group group = m_crbAddToLeft.get_group();
    m_crbAddToRight.set_group(group);
    m_crbAddToRight.set_active();
    m_crbAddToDown.set_group(group);
    m_crbAddToUp.set_group(group);
    m_crbAddToTopLeft.set_group(group);
    m_crbAddToTopRight.set_group(group);
    m_crbAddToBottomLeft.set_group(group);
    m_crbAddToBottomRight.set_group(group);

    m_cvbAddToBox.pack_start(m_crbAddToLeft);
    m_cvbAddToBox.pack_start(m_crbAddToRight);
    m_cvbAddToBox.pack_start(m_crbAddToDown);
    m_cvbAddToBox.pack_start(m_crbAddToUp);
    m_cvbAddToBox.pack_start(m_crbAddToTopLeft);
    m_cvbAddToBox.pack_start(m_crbAddToTopRight);
    m_cvbAddToBox.pack_start(m_crbAddToBottomLeft);
    m_cvbAddToBox.pack_start(m_crbAddToBottomRight);
    m_cfAddToFrame.add(m_cvbAddToBox);

    m_refImageList = Gtk::ListStore::create(m_imageListItem);
    m_ccbImages.set_model(m_refImageList);

    m_ccbImages.pack_start(m_imageListItem.m_sName);
    m_ccbSprites.pack_start(m_spriteListItem.m_iId);

    m_ccbImages.signal_changed().connect( sigc::mem_fun(*this,
        &MainWindow::on_imageCombo_changed) );
    m_ccbSprites.signal_changed().connect( sigc::mem_fun(*this,
        &MainWindow::on_spriteCombo_changed) );
    m_cbAddSpriteToScene.signal_clicked().connect(sigc::mem_fun(*this,
        &MainWindow::on_addSpriteToScene_button_clicked) );
    m_crbAddToLeft.signal_clicked().connect(sigc::mem_fun(*this,
        &MainWindow::on_addSpriteToSide_RadioButton_clicked) );
    m_crbAddToRight.signal_clicked().connect(sigc::mem_fun(*this,
        &MainWindow::on_addSpriteToSide_RadioButton_clicked) );
    m_crbAddToDown.signal_clicked().connect(sigc::mem_fun(*this,
        &MainWindow::on_addSpriteToSide_RadioButton_clicked) );
    m_crbAddToUp.signal_clicked().connect(sigc::mem_fun(*this,
        &MainWindow::on_addSpriteToSide_RadioButton_clicked) );
    m_crbAddToTopLeft.signal_clicked().connect(sigc::mem_fun(*this,
        &MainWindow::on_addSpriteToSide_RadioButton_clicked) );
    m_crbAddToTopRight.signal_clicked().connect(sigc::mem_fun(*this,
        &MainWindow::on_addSpriteToSide_RadioButton_clicked) );
    m_crbAddToBottomLeft.signal_clicked().connect(sigc::mem_fun(*this,
        &MainWindow::on_addSpriteToSide_RadioButton_clicked) );
    m_crbAddToBottomRight.signal_clicked().connect(sigc::mem_fun(*this,
        &MainWindow::on_addSpriteToSide_RadioButton_clicked) );

    m_tool_new.signal_clicked().connect(sigc::mem_fun(*this,
        &MainWindow::on_clear) );
    m_tool_save.signal_clicked().connect(sigc::mem_fun(*this,
        &MainWindow::on_dump) );
    m_tool_add_image.signal_clicked().connect( sigc::mem_fun(*this,
        &MainWindow::on_addImage_button_clicked) );
    m_tool_add_sprite.signal_clicked().connect( sigc::mem_fun(*this,
         &MainWindow::on_addSprite_button_clicked) );
    m_tool_open.signal_clicked().connect( sigc::mem_fun(*this,
         &MainWindow::on_open) );

    show_all_children();
}

void MainWindow::on_addSpriteToSide_RadioButton_clicked()
{
    if (m_crbAddToLeft.get_active()) {
        m_iAddSpriteSide = LEFT;
    } else if (m_crbAddToRight.get_active()) {
        m_iAddSpriteSide = RIGHT;
    } else if (m_crbAddToDown.get_active()) {
        m_iAddSpriteSide = BOTTOM;
    } else if (m_crbAddToUp.get_active()) {
        m_iAddSpriteSide = TOP;
    } else if (m_crbAddToTopLeft.get_active()) {
        m_iAddSpriteSide = TOP_LEFT;
    } else if (m_crbAddToTopRight.get_active()) {
        m_iAddSpriteSide = TOP_RIGHT;
    } else if (m_crbAddToBottomLeft.get_active()) {
        m_iAddSpriteSide = BOTTOM_LEFT;
    } else if (m_crbAddToBottomRight.get_active()) {
        m_iAddSpriteSide = BOTTOM_RIGHT;
    }
    cout << m_iAddSpriteSide << endl;
}

/*
void MainWindow::on_selectRect_button_clicked()
{
    m_cMyDrawingArea.m_srect.m_visible = m_ctbSelectionRect.get_active();
    m_cMyDrawingArea.repaint();
}
*/

void MainWindow::on_addSpriteToScene_button_clicked()
{
    //uint currentLayerIndex = 0;
    //Layer_List &l = Layer_List::get_instance();

    Gtk::TreeModel::iterator iter = m_ccbImages.get_active();
    if (iter) {
        Gtk::TreeModel::Row imageInfo = *iter;
        uint imageId = imageInfo[m_imageListItem.m_iId];

        Gtk::TreeModel::iterator iter = m_ccbSprites.get_active();
        if(iter) {
            Gtk::TreeModel::Row spriteInfo = *iter;
            uint spriteId = spriteInfo[m_spriteListItem.m_iId];
            SDL_Rect rect = spriteInfo[m_spriteListItem.m_rect];

            if (Debug::test(DEBUG_INFO)) {
                cout << "Selected sprite to add: " << rect.x << " " << rect.y << " " <<
                    rect.w << " " << rect.h << endl;
            }

            uint activeSpriteInstanceIndex;
            uint activeSpriteInstanceImageIndex;
            uint activeSpriteInstanceSpriteIndex;
            SDL_Rect activeSpriteRect;
            if(m_currentCtx.getActiveSpriteInfo(activeSpriteInstanceIndex,
                activeSpriteInstanceImageIndex, activeSpriteInstanceSpriteIndex,
                activeSpriteRect)) {

                if (m_iAddSpriteSide == LEFT) {
                    rect.x = activeSpriteRect.x - rect.w;
                    rect.y = activeSpriteRect.y;
                    cout << "left" << endl;
                } else if (m_iAddSpriteSide == RIGHT) {
                    rect.x = activeSpriteRect.x + activeSpriteRect.w;
                    rect.y = activeSpriteRect.y;
                    cout << "right" << endl;
                } else if (m_iAddSpriteSide == TOP) {
                    rect.x = activeSpriteRect.x;
                    rect.y = activeSpriteRect.y - rect.h;
                    cout << "top" << endl;
                } else if (m_iAddSpriteSide == BOTTOM) {
                    rect.x = activeSpriteRect.x;
                    rect.y = activeSpriteRect.y + activeSpriteRect.h;
                    cout << "bottom" << endl;
                } else if (m_iAddSpriteSide == TOP_LEFT) {
                    rect.x = activeSpriteRect.x - rect.w;
                    rect.y = activeSpriteRect.y - rect.h;
                    cout << "top left" << endl;
                } else if (m_iAddSpriteSide == TOP_RIGHT) {
                    rect.x = activeSpriteRect.x + activeSpriteRect.w;
                    rect.y = activeSpriteRect.y - rect.h;
                    cout << "top right" << endl;
                } else if (m_iAddSpriteSide == BOTTOM_LEFT) {
                    rect.x = activeSpriteRect.x - rect.w;
                    rect.y = activeSpriteRect.y + activeSpriteRect.h;
                    cout << "bottom left" << endl;
                } else if (m_iAddSpriteSide == BOTTOM_RIGHT) {
                    rect.x = activeSpriteRect.x + activeSpriteRect.w;
                    rect.y = activeSpriteRect.y + activeSpriteRect.h;
                    cout << "bottom right" << endl;
                }
            }
            addSpriteToLayer(imageId, spriteId, rect, m_ccbSpriteVisible.get_active(), m_ccbSpriteSolid.get_active());
        } else {
            cout <<"No Sprite Instance Added : Invalid iterator" << endl;
            return;
        }
    } else {
        cout << "No Sprite Instance Added : Invalid iterator" << endl;
        return;
    }
}

void MainWindow::addSpriteToLayer(uint imgId, uint spriteId, SDL_Rect rect, bool visible, bool solid)
{
    Layer &layer = m_layerList[m_currentCtx.getCurrentLayerIndex()];
    layer.add(imgId, spriteId, rect, visible, solid);
}

void MainWindow::on_addSprite_button_clicked()
{
    SDL_Rect r;
    r.x = m_cMyDrawingArea.m_srect.m_x1;
    r.y = m_cMyDrawingArea.m_srect.m_y1;
    r.w = m_cMyDrawingArea.m_srect.m_x2 - m_cMyDrawingArea.m_srect.m_x1;
    r.h = m_cMyDrawingArea.m_srect.m_y2 - m_cMyDrawingArea.m_srect.m_y1;
    addSprite(r);
}

void MainWindow::on_addImage_button_clicked()
{
    Gtk::FileChooserDialog dialog("Please choose a file",
    Gtk::FILE_CHOOSER_ACTION_OPEN);
    dialog.set_transient_for(*this);
    dialog.set_current_folder(m_strLastVisitedFolder);
    //Add response buttons the the dialog:
    dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
    dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);

    Gtk::FileFilter filter_img;
    filter_img.set_name("PNG Images");
    filter_img.add_mime_type("image/png");
    dialog.add_filter(filter_img);
/*
    Gtk::FileFilter filter_any;
filter_any.set_name("Any files");
filter_any.add_pattern("*");
dialog.add_filter(filter_any);
*/
    //Show the dialog and wait for a user response:
    int result = dialog.run();
    //Handle the response:
    switch(result)
    {
        case(Gtk::RESPONSE_OK):
        {
            std::cout << "Open clicked." << std::endl;
            string filename = dialog.get_filename();
            m_strLastVisitedFolder.assign(filename, 0, filename.find_last_of('/'));
            cout << "last visited folder " << m_strLastVisitedFolder << endl;
            addImage(filename);
            break;
        }
        case(Gtk::RESPONSE_CANCEL):
        {
            std::cout << "Cancel clicked." << std::endl;
        break;
        }
        default:
        {
            std::cout << "UneXpected button clicked." << std::endl;
        break;
        }
    }
    if (Debug::test(DEBUG_INFO)) {
        cout << "done with image Add" << endl;
    }
}

void MainWindow::addImage(string filename)
{
    Gtk::TreeModel::Row imageInfo = *(m_refImageList->append());
    m_imageCount++;
    imageInfo[m_imageListItem.m_iId] = m_imageIndexCounter++;
    imageInfo[m_imageListItem.m_sPath] = filename;
    imageInfo[m_imageListItem.m_spriteCount] = 0;
    imageInfo[m_imageListItem.m_spriteIndexCounter] = 0;
    imageInfo[m_imageListItem.m_refSpriteList] = Gtk::ListStore::create(m_spriteListItem);

    string str;

    uint start = filename.find_last_of('/') + 1;
    uint end = filename.find_last_of('.');

    str.assign(filename, start , end - start);
    cout << endl << str << endl;

    imageInfo[m_imageListItem.m_sName] = str;

    cout << "File selected: " << filename << std::endl;
    Glib::RefPtr<Gdk::Pixbuf> image = Gdk::Pixbuf::create_from_file(filename);
    imageInfo[m_imageListItem.m_refImage] = image;
    m_ccbImages.set_active(imageInfo);
    cout << "Going to call ImageList::add()" << endl;
    m_imgList.add(imageInfo[m_imageListItem.m_iId], filename);
    cout << "Finished ImageList::add()" << endl;

    if (Debug::test(DEBUG_INFO)) {
        cout << "done with image Add" << endl;
    }
}

void MainWindow::on_imageCombo_changed()
{
    Gtk::TreeModel::iterator iter = m_ccbImages.get_active();
    if(iter)
    {
        Gtk::TreeModel::Row imageInfo = *iter;
        if(imageInfo)
        {
            //Get the data for the selected row, using our knowledge of the tree
            //model:
            int id = imageInfo[m_imageListItem.m_iId];
            Glib::ustring path = imageInfo[m_imageListItem.m_sPath];
            if (Debug::test(DEBUG_INFO)) {
                std::cout << " IID=" << id << ", name=" << path << std::endl;
            }
            Glib::RefPtr<Gdk::Pixbuf> image = imageInfo[m_imageListItem.m_refImage];
            m_cMyDrawingArea.setImage(image, 0, 0, image->get_width(), image->get_height());
            m_cMyDrawingArea.repaint();

            Glib::RefPtr<Gtk::ListStore> refSpriteList = imageInfo[m_imageListItem.m_refSpriteList];

            m_ccbSprites.set_model(refSpriteList);

            m_ccbSprites.set_active(0);
        }
    }
    else {
        std::cout << "invalid iter or no image" << std::endl;
    }
}

void MainWindow::on_spriteCombo_changed()
{
    Gtk::TreeModel::iterator imageIter = m_ccbImages.get_active();
    if(imageIter) {
        Gtk::TreeModel::Row imageInfo = *imageIter;
        if(imageInfo) {
            int id = imageInfo[m_imageListItem.m_iId];
            Glib::ustring path = imageInfo[m_imageListItem.m_sPath];
            if (Debug::test(DEBUG_INFO)) {
                std::cout << " SID=" << id << ", name=" << path << std::endl;
            }
            Glib::RefPtr<Gdk::Pixbuf> image = imageInfo[m_imageListItem.m_refImage];
//            m_cMyDrawingArea.setImage(image, 0, 0, image->get_width(), image->get_height());
//            m_cMyDrawingArea.repaint();
            Gtk::TreeModel::iterator spriteIter = m_ccbSprites.get_active();
            if(spriteIter) {
                Gtk::TreeModel::Row spriteInfo = *spriteIter;
                if(spriteInfo) {
                    SDL_Rect r = spriteInfo[m_spriteListItem.m_rect];
                    m_csaSprite.setImage(image, r.x, r.y, r.w, r.h);
                } else {
                    std::cout << "no sprite present" << endl;
                }
            } else {
                std::cout << "invalid iter1" << std::endl;
            }
        } else {
            std::cout << "invalid iter1" << std::endl;
        }
    }
}

void MainWindow::on_dump()
{
    Dumper dumper;
    dumper.collect_and_dump();
}

void MainWindow::clear_image_data()
{
    typedef Gtk::TreeModel::Children ROWS;
    ROWS rows = m_refImageList->children();

    for(ROWS::iterator iter = rows.begin(); iter != rows.end(); ++iter) {
        Gtk::TreeModel::Row imageInfo = *iter;
        if(imageInfo)
        {
            Glib::RefPtr<Gtk::ListStore> refSpriteList = imageInfo[m_imageListItem.m_refSpriteList];
            refSpriteList->clear();
            Glib::RefPtr<Gdk::Pixbuf> imagebuff = imageInfo[m_imageListItem.m_refImage];
        }
    }
    m_refImageList->clear();
}

void MainWindow::on_clear()
{
    m_imageCount = 0;
    m_imageIndexCounter = 0;
    clear_image_data();
    m_layerList.clear();
    m_imgList.clear();
    m_layerListUi.clear();
    m_currentCtx.setCurrentLayerIndex(INVALID_LAYER_ID);
    m_currentCtx.invalidateActiveSpriteInfo();
    ID_Manager::get_instance().reset();
}

void MainWindow::on_open()
{
    on_clear();

    MAP_IID_IMAGEDATA images;
    MAP_IID_MAP_SID_SPRITEDATA sprites;
    char imageId[10];
    char spriteId[10];
    char line[256];
    char path[256];
    SDL_Rect rect;
    int imageCounter = 0;
    int spriteCounter = 0;
    int iid;
    ImageData imd;
    SpriteData sd;

    ifstream infile("out1.txt");
    if(!infile) {
        cout << "cannot open dump" << endl;
        return;
    } else {
        cout << "out1file opened successfully" << endl;
    }

    while(!infile.eof()) {
        infile.getline(line, 256);
        if(!strncmp(line, "images", 6)) {
            sscanf(line, "images[%8s] = new Image_Wrapper(\"%[^\"]s", imageId, path);
            imd.path = path;
            images.insert(PAIR_IID_IMAGEDATA(string(imageId), imd));
            cout << "image id " << imd.imageId << " " << path << endl;
        } else if(!strncmp(line, "sprites", 7)) {
            SpriteData s;
            sscanf(line, "sprites[%8s] = new Sprite(%8s, %hd, %hd, %hd, %hd", spriteId, imageId, &s.rect.x, &s.rect.y, &s.rect.w, &s.rect.h);
            MAP_IID_MAP_SID_SPRITEDATA::iterator itr = sprites.find(imageId);
            if (itr == sprites.end()) {
                sprites.insert(PAIR_IID_MAP_SID_SPRITEDATA(imageId, MAP_SID_SPRITEDATA()));
                cout << "reset sprite counter" << spriteCounter << endl;
            }

            MAP_SID_SPRITEDATA &inner = sprites[imageId];
            inner.insert(PAIR_SID_SPRITEDATA(string(spriteId), s));
            cout << "Read sprite " << images[imageId].imageId  << " " << s.spriteId << " " << s.rect.x << " " << s.rect.y << " " << s.rect.w << " " << s.rect.h << endl;
        } else if(!strncmp(line, "==end==", 7)) {
            break;
        }
    }

    MAP_IID_MAP_SID_SPRITEDATA::iterator itr;

    for(itr = sprites.begin(); itr != sprites.end(); itr++) {
        images[itr->first].imageId = imageCounter++;
        addImage(images[itr->first].path);
        MAP_SID_SPRITEDATA::iterator i;
        spriteCounter = 0;
        for(i = (itr->second).begin(); i != (itr->second).end(); i++) {
            i->second.spriteId = spriteCounter++;
            addSprite(i->second.rect);
            cout << "AAdded sprite " << i->second.rect.x << " " << i->second.rect.y << " "
                << i->second.rect.w << " " << i->second.rect.h << endl;
        }
    }

    infile.close();
    infile.open("out.txt");
    if(!infile) {
        cout << "cannot open dump" << endl;
        return;
    } else {
        cout << "outfile opened successfully" << endl;
    }

    while(!infile.eof()) {
        infile.getline(line, 256);
        if(!strncmp(line, "//layer", 7)) {
            m_layerListUi.on_addLayer_Button_clicked();
            cout << "create new layer" << endl;
        } else if(!strncmp(line, "initInstance", 12)) {
            int visible, solid;
            int useless;
            sscanf(line, "initInstance(%d, %8s, %8s, %hd, %hd, %d, %d, %d, %d", &useless, imageId, spriteId, &rect.x, &rect.y, &useless, &useless, &visible, &solid);
            iid = images[imageId].imageId;
            sd = (sprites[imageId])[spriteId];
            rect.w = sd.rect.w;
            rect.h = sd.rect.h;
            cout << "instance imgid = " << iid <<" sprite id = " << sd.spriteId
            << " " << rect.x << " "
            << " " << rect.y << " "
            << " " << rect.w << " "
            << " " << rect.h << endl;
            addSpriteToLayer(iid, sd.spriteId, rect, visible, solid);
        }
    }
    infile.close();
}

